/**
 * 
 */
package app.dev.telemetric;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import app.dev.telemetric.model.Log;

/**
 * @author David
 *
 */
public class EmailAlert extends Thread 
{
	//Constantes
	private static final String TAG=EmailAlert.class.getSimpleName();
	private static final String MAIL_SMTP_AUTH="mail.smtp.auth";
	private static final String MAIL_SMTP_STARTTLS_ENABLE="mail.smtp.starttls.enable";
	private static final String MAIL_SMTP_HOST="mail.smtp.host";
	private static final String MAIL_SMTP_PORT="mail.smtp.port";
	
	//Alertas habilitadas
	private boolean enable=false;
	//remitente
	private String from="";
	//servidor smtp
	private String smtp="";
	//puerto del servicio smtp
	private String smtp_port="";
	//destinatario
	private String to="";
	//Sesion Smtp
	private Authenticator auth=null;
	//Contenido del mensaje
	private Log message_body;
	
	/**
	 * Construcor del generador de alertas por email
	 */
	public EmailAlert()
	{
		super(TAG);
	}

	/**
	 * Crea el cuerpo del email a partir del log
	 * @return: devuelve el cuerpo del email
	 */
	private String GenerateBody() 
	{
		String ret="";
		
		if(message_body!=null)
		{
			ret+="DATE: "+message_body.getDate().toString()+"\n";
			ret+="TAG: "+message_body.getTag()+"\n";
			if(message_body.getException()!=null)
				ret+="EXCEPTION: "+message_body.getException()+"\n";
			if(message_body.getExceptioncause()!=null)
				ret+="EXCEPTION CAUSE: "+message_body.getExceptioncause()+"\n";
			if(message_body.getExceptionmessage()!=null)
				ret+="EXCEPTION MESSAGE: "+message_body.getExceptionmessage()+"\n";
			ret+="DESCRIPTION: "+message_body.getMessage();
		}
		
		return ret;
	}
	
	@Override
	public void run()
	{
		Properties props = new Properties();
		Session session;
		Message message;
		
		if(!enable)
			return;
			
		props.put(MAIL_SMTP_AUTH, Boolean.TRUE.toString());
		props.put(MAIL_SMTP_STARTTLS_ENABLE, Boolean.TRUE.toString());
		props.put(MAIL_SMTP_HOST, smtp);
		props.put(MAIL_SMTP_PORT, smtp_port);
		
		if(auth==null)
			return;
		
		session = Session.getInstance(props, auth);
		
		try {
			 
			message = new MimeMessage(session);
			message.setFrom(new InternetAddress(from));
			message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
			message.setSubject(message_body.getType());
			message.setText(GenerateBody());
 
			Transport.send(message);
 
		} catch (MessagingException e) {
			return;
		}
	}

	/**
	 * @return the enable
	 */
	public boolean isEnable() {
		return enable;
	}

	/**
	 * @param enabled the enable to set
	 */
	public void setEnable(boolean enable) {
		this.enable = enable;
	}

	/**
	 * @return the from
	 */
	public String getFrom() {
		return from;
	}

	/**
	 * @param from the from to set
	 */
	public void setFrom(String from) {
		this.from = from;
	}

	/**
	 * @return the smtp
	 */
	public String getSmtp() {
		return smtp;
	}

	/**
	 * @param smtp the smtp to set
	 */
	public void setSmtp(String smtp) {
		this.smtp = smtp;
	}

	/**
	 * @return the smtp_port
	 */
	public String getSmtp_port() {
		return smtp_port;
	}

	/**
	 * @param smtp_port the smtp_port to set
	 */
	public void setSmtp_port(String smtp_port) {
		this.smtp_port = smtp_port;
	}

	/**
	 * @return the to
	 */
	public String getTo() {
		return to;
	}

	/**
	 * @param to the to to set
	 */
	public void setTo(String to) {
		this.to = to;
	}

	/**
	 * @return the auth
	 */
	public Authenticator getAuth() {
		return auth;
	}

	/**
	 * @param session the auth to set
	 */
	public void setAuth(Authenticator auth) {
		this.auth = auth;
	}

	/**
	 * @return the message_body
	 */
	public Log getMessage_body() {
		return message_body;
	}

	/**
	 * @param message_body the message_body to set
	 */
	public void setMessage_body(Log message_body) {
		this.message_body = message_body;
	}
}
